package club.ximeng.javafxspringboot.views;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.stage.Window;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;

/**
 * 主界面控制器
 *
 * @author liu xm
 * @date 2022-02-17 1:57
 */
@Slf4j
@FXMLController
public class MainController implements Initializable {
    @FXML
    private Label welcomeText;
    @FXML
    public StackPane rootStackPane;
    @FXML
    public Button btnAlert;
    @FXML
    public Button btnChooseFile;


    @FXML
    protected void onHelloButtonClick() {
        welcomeText.setText("Welcome to JavaFx Quickly Start!");
    }

    /**
     * 弹出框按钮单击事件
     *
     * @param actionEvent .
     */
    public void onAlertButtonClick(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setContentText(DateUtil.now());
        alert.show();
    }

    /**
     * 选择文件按钮单机事件
     *
     * @param actionEvent .
     */
    public void onChooseFileButtonClick(ActionEvent actionEvent) {
        Window window = rootStackPane.getScene().getWindow();
        FileChooser fileChooser = new FileChooser();
        // 文件类型过滤器
        FileChooser.ExtensionFilter filter = new FileChooser
                .ExtensionFilter("Excel files (*.xls, *.xlsx)", "*.xls", "*.xlsx");
        fileChooser.getExtensionFilters().add(filter);
        File file = fileChooser.showOpenDialog(window);
        String fileName = FileUtil.getName(file);
        String fileAbsolutePath = FileUtil.getAbsolutePath(file);
        if (Objects.nonNull(file)) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setContentText("文件名称：" + fileName + "\n文件路径：" + fileAbsolutePath);
            alert.show();
        }
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        log.info("initialize: {}", location.getPath());
    }

}
